package chen.web.controller.forum;

import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import chen.web.favorite.Favorite;
import chen.web.favorite.FavoriteManager;
import chen.web.forum.Reply;
import chen.web.forum.Subject;
import chen.web.forum.TopicManager;


public class FavoriteController {

	private @Autowired FavoriteManager fm;
	private @Autowired TopicManager tm;
	
	//删除收藏
	@RequestMapping(value="/favorite/{ids}", method=RequestMethod.DELETE)
	public @ResponseBody JSONResponse delete(@RequestParam String ids) throws JSONException{
		String[] idArr= ids.split("_");
		for(String id : idArr){
			Favorite favorite = fm.getFavorite(Integer.parseInt(id));
			if(favorite != null){
				fm.delete(favorite);	
			}
		}
		return JSONResponse.successed();
	}

	//收藏主题
	@RequestMapping(value="/favorite/topic/{id}", method=RequestMethod.POST)
	public @ResponseBody JSONResponse favoriteTopic(@PathVariable int id){
		Subject subject = tm.getSubject(id);
		fm.add(subject);
		return JSONResponse.successed();
	}

	//收藏回复
	@RequestMapping(value="/favorite/reply/{id}", method=RequestMethod.POST)
	public @ResponseBody JSONResponse favoriteReply(@PathVariable int id){
		Reply reply = tm.getReply(id);
		fm.add(reply);
		return JSONResponse.successed();
	}

}